package com.jackrain.nea.fc.dl.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewDlPurOutBillIdUnsendCmd;
import com.jackrain.nea.fc.dl.mapper.ViewDLTPurOutBillIdMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.util.HashMap;


/**
 *  供应商发货单取消发出
 *
 * @author kangcong
 * @date 2018/08/14
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewDlPurOutBillIdUnsendCmdImpl extends CommandAdapter  implements ViewDlPurOutBillIdUnsendCmd {

//    @Autowired
//    private PurOutBillIdPushEsService purOutBillIdPushEsService;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
//        log.debug("------------ViewDlPurOutBillIdUnsendCmdImpl==" + map.toString());
        log.info("ViewDlPurOutBillIdUnsendCmdImpl--map-->{}",map);

        ValueHolder vh = new ValueHolder();
        ViewDLTPurOutBillIdMapper viewDLTPurOutBillIdMapper = ApplicationContextHandle.getBean(ViewDLTPurOutBillIdMapper.class);

        JSONObject param = null;
        try {
            param = JSON.parseObject(map.get("param").toString());
        } catch (Exception e) {
            vh.put("CODE", "-1");
            vh.put("MSG", "请求参数转json错误==" + map.toString());
            e.printStackTrace();
            return vh;
        }

        if (param == null || param.size() == 0) {
            vh.put("CODE", "-1");
            vh.put("MSG", "参数转json后为空==" + map.toString());
            return vh;
        }
//        log.debug("------------ubsendjsonobject==" +param.toString());

        String billNo = param.getString("OUT_BILL_NO");
        String modifiername = param.getString("MODIFIERNAME");


        if (billNo == null || modifiername == null) {
            vh.put("CODE", "-1");
            vh.put("MSG", "请求参数不正确==" + map.toString());
            return vh;
        }

        try {
            int count = viewDLTPurOutBillIdMapper.update(billNo, modifiername, new Timestamp(System.currentTimeMillis()).toString());

            if(count != 0){
//                //通过单号查询数据，推ES
//                HashMap data = viewDLTPurOutBillIdMapper.selectDataByOutBillNo(billNo);
//                JSONObject esdata = new JSONObject(data);
//                purOutBillIdPushEsService.push(esdata, esdata.getLong("ID"));

                vh.put("CODE", "1");
                vh.put("MSG", "success");
            } else {
                throw new NDSException("更新数据库影响条数为0");
            }

        } catch (Exception e) {
            vh.put("CODE", "-1");
            vh.put("MSG", "取消发出操作数据库失败");

            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            if ((e instanceof NDSException)) {
                throw e;
            } else {
                throw new NDSException(sw.toString());
            }
        }

        return vh;
    }
}
